package cn.maihe.elg.operation.supports.guarantee;

import cn.maihe.elg.operation.supports.guarantee.dto.*;
import cn.maihe.elg.operation.supports.guarantee.enums.GuaranteeCode;
import cn.maihe.elg.operation.utils.MD5FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Description 保函校验接口
 * @Author caixh
 * @Date 2021/5/18.
 */
@Api(value = "保函校验接口", tags = "保函校验接口")
@RestController
@RequestMapping("/guarantee/check")
@Slf4j
public class GuaranteeCheckController {
    @Resource
    private GuaranteeServiceFactory guaranteeServiceFactory;


    private GuaranteeServiceInterface serviceInterface(GuaranteeCode guaranteeCode) {
        return guaranteeServiceFactory.getGuaranteeService(guaranteeCode);
    }


    @ApiOperation(value = "保函信息获取", notes = "保函信息获取")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public BaseResponseDTO<GuaranteeInfoRespDTO> info(@Validated @RequestBody GuaranteeInfoReqDTO reqDTO) {

        if(StringUtils.isEmpty(reqDTO.getGuaranteeNo())){
            return BaseResponseDTO.exceptionParam("保函编号为空");
        }
        if(StringUtils.isEmpty(reqDTO.getCreditCode())){
            return BaseResponseDTO.exceptionParam("保函投标人信用统一码为空");
        }
        GuaranteeCode guaranteeCode = GuaranteeCode.resolve(reqDTO.getChannel());
        return serviceInterface(guaranteeCode).guaranteeInfo(reqDTO);
    }

    @ApiOperation(value = "保函文件查验", notes = "保函文件查验")
    @RequestMapping(value = "/fileCheck", method = RequestMethod.POST)
    public BaseResponseDTO<FileCheckRespDTO> fileCheck(@RequestParam("file") MultipartFile file, FileCheckResDTO reqDTO){

        if(file.isEmpty()){
            return BaseResponseDTO.exceptionParam("保函文件为空");
        }
        GuaranteeCode guaranteeCode = GuaranteeCode.resolve(reqDTO.getChannel());
        //获取md5
        try {
            String md5= MD5FileUtil.getFileMD5(file);
            return serviceInterface(guaranteeCode).fileCheck(md5,reqDTO);
        } catch (IOException e) {
            return BaseResponseDTO.exception("保函文件异常");
        }
    }
}
